<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: tracking.php 135 2013-09-22 07:30:55Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 135 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-22 07:30:55 +0000 (Sun, 22 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

$ref_url = parse_url(@$_SERVER['HTTP_REFERER']);

if ($_SESSION['tracked'] != true) {
	$_SESSION['tracking']['http_referer'] = $ref_url;
	$_SESSION['tracked'] = true;
}

if (!isset($_SESSION['tracking']['ip'])) 
	$_SESSION['tracking']['ip'] = $_SERVER['REMOTE_ADDR'];
	
if (!isset($_SESSION['tracking']['refID']) || $_SESSION['tracking']['refID'] == "") {
	if(isset($_GET['refID']) && $_GET['refID'] != "") {
		$campaign_check = $db->db_query("SELECT
												*
											FROM
												".TABLE_CAMPAIGNS."
			                            	WHERE
			                            		campaigns_refID = ".$db->db_prepare($_GET['refID'])."");

		if($campaign_check->_numOfRows == 1) {
			$_SESSION['tracking']['refID'] = $_GET['refID'];
			$insert_sql = array('user_ip' => $_SESSION['tracking']['ip'],
								'campaign' => $_GET['refID'],
								'time' => 'NOW()');
			$check_date = mktime(0, date("i")-1, 0,date("m"), date("d"), date("Y"));
			$ip_query = $db->db_query("	SELECT
											*
										FROM
											".TABLE_CAMPAIGNS_IP."
										WHERE
											campaign = ".$db->db_prepare($_GET['refID'])."
										AND
											user_ip = '".$_SESSION['tracking']['ip']."'
										AND
											time > '".$check_date."'");
			if(!$ip_query->_numOfRows)
				$db->db_perform(TABLE_CAMPAIGNS_IP, $insert_sql);
		}
	}
}

if(!isset($_SESSION['tracking']['date']))
	$_SESSION['tracking']['date'] = (date("Y-m-d H:i:s"));

if(!isset($_SESSION['tracking']['browser']))
	$_SESSION['tracking']['browser'] = $_SERVER["HTTP_USER_AGENT"];

$i = count($_SESSION['tracking']['pageview_history']);

if ($i > 6) {
	array_shift($_SESSION['tracking']['pageview_history']);
	$_SESSION['tracking']['pageview_history'][6] = $ref_url;

} else
	$_SESSION['tracking']['pageview_history'][$i] = $ref_url;

if(isset($_SESSION['tracking']['pageview_history'][$i]) && $_SESSION['tracking']['pageview_history'][$i] == $_SESSION['tracking']['http_referer'])
	array_shift($_SESSION['tracking']['pageview_history']);
